package com.juzhencms.apps.busi.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.support.hsf.HSFJSONUtils;
import com.juzhencms.apps.base.common.config.Config;
import com.juzhencms.apps.base.common.database.DBTable;

@Service
public class MenuService extends CommonService {
	
	/*public String getLink(Map param) {
		String module=null;
		if (param.get("id")!=null) {
			
			int id= new Integer( param.get("id").toString());
			String link="/menu_" +id ;
			if(param.get("link_url")!=null && !param.get("link_url").equals("")){
				link=param.get("link_url").toString();
				
				Pattern p=Pattern.compile("^(http|https)",Pattern.CASE_INSENSITIVE);//第二个参数代表不区分大小写,可以同时使用多种，例如 Pattern.CASE_INSENSITIVE | Pattern.MULTILINE
				Matcher m=p.matcher(link);
				boolean result=m.matches();//此方式表示是否整体匹配	
				if(result)
					return link;
				
			}else if(param.get("template")!=null && !param.get("template").equals("")){
				link= "/menu_" +id ;
			}else if(param.get("module")!=null){
				module=param.get("module").toString();
				switch(module){
					case "news":
						link= "/newslist_" +id ;
		                break;
		            case "article":
		            	link= "/article_" +id ;
		                break;
		            case "links":
		            	link= "/linkslist_" +id ;
		                break;
		            default:
		            	link= "/menu_" +id ;
		                break;
		        }
			}
			String siteUrlPath=Config.getSiteUrlPath();
			return siteUrlPath+link;
		}
		return null;
	}*/

	
	/*public Map mainMenu(Map param, HttpServletRequest request) {
		if(param==null)
			param=new HashMap();
		// TODO Auto-generated method stub
		int num=8;//显示的菜单数
		if(param.get("num")!=null)
			num=new Integer(param.get("num").toString());
		
		boolean focus=true;//是否聚焦
		if(param.get("focus")!=null)
			focus=new Boolean(param.get("focus").toString());
		
		//获取当前栏目
		Map localMenu=(Map)request.getAttribute("localMenu");
		Map site=(Map)request.getAttribute("localSite");
		
		Map parentMenu=null;
		
		List pMenuList=new ArrayList();
		Map result=new HashMap();
		
		List menuList=null;
		if(localMenu!=null){
			//获取所传参数的顶级栏目
			parentMenu=localMenu;
			parentMenu.put("link", this.getLink(parentMenu));
			pMenuList.add(parentMenu);

			int parent_id=new Integer(parentMenu.get("parent_id").toString());
			while(parent_id>0){
				parentMenu=(Map)this.getJdbcDao().queryRow("select * from "+DBTable.menu+" where id=?", new Object[]{parent_id});
				parentMenu.put("link", this.getLink(parentMenu));
				pMenuList.add(parentMenu);
				parent_id=new Integer(parentMenu.get("parent_id").toString());
			}
			
			//判断顶级栏目是否有logo
			if(parentMenu.get("independent_menu")!=null && new Integer(parentMenu.get("independent_menu").toString())==1){
				
				result.put("parentMenu", parentMenu);
				//显示子菜单
				menuList=this.getJdbcDao().querySql("select id,name,module,image,link_url,link_target,independent_menu,show_submenu,image,template from "+DBTable.menu+" where parent_id=? and publish='1' and del_flag=0 order by position asc,id asc limit 0,?", new Object[]{parentMenu.get("id"),num});
				
			}
			
		}
		
		if(menuList==null) {
			menuList=this.getJdbcDao().querySql("select id,name,module,image,link_url,link_target,independent_menu,show_submenu,image,template from "+DBTable.menu+" where site_id=? and parent_id=0 and publish='1' and del_flag=0 order by position asc,id asc limit 0,?", new Object[]{site.get("id"),num});					
		}
		
		if(menuList!=null) {
			for(int i=0;i<menuList.size();i++){
				Map downMenu=(Map) (menuList.get(i));
				if (downMenu.get("show_submenu")!=null && new Integer(downMenu.get("show_submenu").toString())==1) {
					String downId= downMenu.get("id").toString();
					List downLowList=this.getJdbcDao().querySql("select id,name,module,image,link_url,link_target,independent_menu,show_submenu,image,template from "+DBTable.menu+" where parent_id=? and publish='1' and del_flag=0 order by position asc,id asc ", new Object[]{downId});
					if(downLowList!=null)
						for(int j=0;j<downLowList.size();j++){
							Map tmpMenu1=(Map)downLowList.get(j);
							tmpMenu1.put("link", this.getLink(tmpMenu1));
						}
					downMenu.put("sub", downLowList);
					
				
				}
			}
		}
		
		
		
		
		
		
		
		//为导航菜单增加链接
		if(menuList!=null)
			for(int i=0;i<menuList.size();i++){
				Map tmpMenu=(Map)menuList.get(i);
				tmpMenu.put("link", this.getLink(tmpMenu));
				
				if(tmpMenu.get("image")==null || tmpMenu.get("image").equals("")) {
					tmpMenu.put("image","nophoto.jpg");
				}
				tmpMenu.put("image", this.getConfig().getUploadUrlPath()+"/"+tmpMenu.get("image"));	
			}
		
		//设置主导航聚焦
		if(parentMenu!=null && menuList!=null)
		for(int i=0;i<menuList.size();i++){
			Map menu=(Map)menuList.get(i);
			if(pMenuList!=null)
				for(int j=0;j<pMenuList.size();j++){
					Map tmp=(Map)pMenuList.get(j);
					if (menu.get("id")!=null) {
						if(menu.get("id").equals(tmp.get("id"))){
							menu.put("focus", true);
						}
					}
				}
		}
		
		result.put("menuList", menuList);
		
		

		
		
		return result;
	}*/

	
	/*public Map subMenu(Map param, HttpServletRequest request) {
		if(param==null)
			param=new HashMap();
		
		int num=8;//显示的菜单数
		if(param.get("num")!=null)
			num=new Integer(param.get("num").toString());
		
		boolean focus=true;//是否聚焦
		if(param.get("focus")!=null)
			focus=new Boolean(param.get("focus").toString());
		
		//获取当前栏目
		Map localMenu=(Map)request.getAttribute("localMenu");
		Map site=(Map)request.getAttribute("localSite");
		
		
		Map parentMenu=null;
		
		List pMenuList=new ArrayList();
		Map result=new HashMap();
		
		List menuList=null;
		if(localMenu!=null){
			//获取所传参数的顶级栏目
			parentMenu=localMenu;
			parentMenu.put("link", this.getLink(parentMenu));
			pMenuList.add(parentMenu);

			int parent_id=new Integer(parentMenu.get("parent_id").toString());
			while(parent_id>0){
				parentMenu=(Map)this.getJdbcDao().queryRow("select * from "+DBTable.menu+" where id=?", new Object[]{parent_id});
				
				if(parentMenu.get("independent_menu")!=null && new Integer(parentMenu.get("independent_menu").toString())==1){
					break;
				}
				
				parentMenu.put("link", this.getLink(parentMenu));
				pMenuList.add(parentMenu);
				parent_id=new Integer(parentMenu.get("parent_id").toString());
			}
			
			menuList=this.getJdbcDao().querySql("select id,name,module,image,link_url,link_target,template from "+DBTable.menu+" where parent_id=? and publish='1' order by position asc,id asc limit 0,?", new Object[]{parentMenu.get("id"),num});
			result.put("parentMenu", parentMenu);
			result.put("menuList", menuList);
		}
		
		//为导航菜单增加链接
		if(menuList!=null)
			for(int i=0;i<menuList.size();i++){
				Map tmpMenu=(Map)menuList.get(i);
				tmpMenu.put("link", this.getLink(tmpMenu));
				
				if(tmpMenu.get("image")==null || tmpMenu.get("image").equals("")) {
					tmpMenu.put("image","nophoto.jpg");
				}
				tmpMenu.put("image", this.getConfig().getUploadUrlPath()+"/"+tmpMenu.get("image"));	
				
				if(tmpMenu.get("logo")==null || tmpMenu.get("logo").equals("")) {
					tmpMenu.put("logo","nophoto.jpg");
				}
				tmpMenu.put("logo", this.getConfig().getUploadUrlPath()+"/"+tmpMenu.get("logo"));
				
			}
		
		//设置导航聚焦
		if(parentMenu!=null && menuList!=null)
		for(int i=0;i<menuList.size();i++){
			Map menu=(Map)menuList.get(i);
			if(pMenuList!=null)
				for(int j=0;j<pMenuList.size();j++){
					Map tmp=(Map)pMenuList.get(j);
					if(menu.get("id").equals(tmp.get("id"))){
						menu.put("focus", true);
					}
				}
		}
		//LogTool.print(result, this.getClass());
		return result;
	}*/
	
	/*public Map getLocalMenu(int menuId){
		if(menuId==0)
			return null;
		Map tmpMenu=this.getJdbcDao().queryRow("select id,parent_id,site_id,name,module,alias,template,logo,independent_menu,link_url,link_target from "+DBTable.menu+" where id=?",new Object[]{menuId});
		if(tmpMenu!=null){
			if( (tmpMenu.get("template")!=null && !tmpMenu.get("template").equals("")) ||
				(tmpMenu.get("module")!=null && !tmpMenu.get("module").equals("")) ||
				(tmpMenu.get("link_url")!=null && !tmpMenu.get("link_url").equals(""))
					){
				return tmpMenu;
			}else{
				Map subMenu=this.getFirstSubMenu(new Integer(tmpMenu.get("id").toString()));
				if(subMenu!=null){
					return subMenu;
				}else{
					return tmpMenu;
				}
			}
		}else{
			return null;
		}
		
	}
	
	public Map getSubMenuByName(int pid,String name) {
		Map tmpMenu=this.getJdbcDao().queryRow("select id,parent_id,site_id,name,module,alias,template,independent_menu,logo,link_url,link_target from "+DBTable.menu+" where parent_id=? and name=?  limit 0,1",new Object[]{pid,name});
		return tmpMenu;
	}
	
	public Map getFirstSubMenu(int pid){
		Map tmpMenu=this.getJdbcDao().queryRow("select id,parent_id,site_id,name,module,alias,template,independent_menu,logo,link_url,link_target from "+DBTable.menu+" where parent_id=? and publish='1' order by position asc,id asc limit 0,1",new Object[]{pid});
		if(tmpMenu!=null){
			if( (tmpMenu.get("template")!=null && !tmpMenu.get("template").equals("")) ||
					(tmpMenu.get("module")!=null && !tmpMenu.get("module").equals("")) ||
					(tmpMenu.get("link_url")!=null && !tmpMenu.get("link_url").equals(""))
						){
				return tmpMenu;
			}else{
				return this.getFirstSubMenu(new Integer(tmpMenu.get("id").toString()));
				
			}
		}else{
			return null;
		}
		
	}

	
	public void setLocalMenu(int menuId, HttpServletRequest request) {
		// TODO Auto-generated method stub
		Map localMenu=null;
		if(menuId>0){
			localMenu=this.getLocalMenu(menuId);
		}else{
			//设置首页聚焦
			Map localSite=(Map)request.getAttribute("localSite");
			localMenu=this.getJdbcDao().queryRow("select * from "+DBTable.menu+" where site_id=? and template='index' and del_flag=0 limit 0,1", new Object[]{localSite.get("id")});
		}
		
		if(localMenu!=null){
			localMenu.put("link", this.getLink(localMenu));
			request.setAttribute("localMenu", localMenu);
		}
		
		//设置面包屑导航数据
		if(localMenu!=null){
			List pMenuList=new ArrayList();
			Map parentMenu=localMenu;
			
			parentMenu.put("link", this.getLink(parentMenu));
			pMenuList.add(parentMenu);

			int parent_id=new Integer(parentMenu.get("parent_id").toString());
			while(parent_id>0){
				parentMenu=(Map)this.getJdbcDao().queryRow("select * from "+DBTable.menu+" where id=?", new Object[]{parent_id});
				parentMenu.put("link", this.getLink(parentMenu));
				pMenuList.add(parentMenu);
				parent_id=new Integer(parentMenu.get("parent_id").toString());
			}
			
			if(pMenuList.size()>0){
				//父级栏目倒序排序，用作面包屑导航
				Collections.reverse(pMenuList);
				
				request.setAttribute("locationMenuList", pMenuList);
			}
			
			
		}
		
		
	}
	

	
	public List subMenuList(Map param) {
		// TODO Auto-generated method stub
		int num=10;
		if(param.get("num")!=null){
			num=new Integer(param.get("num").toString());
		}
		
		List menuList=null;
		if(param.get("parent_id")!=null){
			menuList=(List)this.getJdbcDao().querySql("select * from "+DBTable.menu+" where parent_id=? and publish=1 and del_flag=0 limit 0,?", new Object[]{param.get("parent_id"),num});
			if(menuList!=null)
				for(int i=0;i<menuList.size();i++){
					Map row=(Map)menuList.get(i);
					row.put("link", this.getLink(row));
				}
		}
		
		return menuList;
	}*/

	

	
	/*public Map getData(int id) {
		Map menu=this.getJdbcDao().queryRow("select * from "+DBTable.menu+" where id=?", new Object[]{id});
		return menu;
	}*/
	
	/*public Map getMenuTreeData(int site_id, int parent_id) {
		// TODO Auto-generated method stub
		List whereParamList=new ArrayList();
		String where=" where site_id= ? and parent_id=? and publish=1 and del_flag=0  ";
		whereParamList.add(site_id);
		whereParamList.add(parent_id);
		
		where+=" order by position asc";
		List menuList = this.getJdbcDao().querySql("select id,name,module,publish,template,link_url from " + DBTable.menu
				+ where, whereParamList.toArray());
		if (menuList != null && menuList.size() > 0) {
			for (int i = 0; i < menuList.size(); i++) {
				Map row = (Map) menuList.get(i);
				
				row.put("link", this.getLink(row));
				
				
				Map tmp = this.getMenuTreeData(site_id,new Integer(row.get("id").toString()));
				if (tmp != null)
					row.put("sub", tmp.get("sub"));
			}
			Map data = new HashMap();
			data.put("sub", menuList);
			return data;
		} else {
			return null;
		}
	}*/
	/**
	 * 向栏目添加测试数据
	 * @param menuId
	 */
	public void addTestData(String id) {
		
		Map menu=this.getJdbcDao().queryRow("select * from "+DBTable.menu+" where id=? and del_flag=0", new Object[] {id});
		
		this.addTestData(menu);
		
		
		
	}
	
	
	/**
	 * 向栏目添加测试数据
	 * @param menuId
	 */
	public void addTestData(Map menu) {
		
		if(menu!=null && menu.get("module")!=null && menu.get("name")!=null && menu.get("site_id")!=null) {
			String moduleName=menu.get("module").toString();
			String menuName=menu.get("name").toString();
			String menuId=menu.get("id").toString();
			String siteId=menu.get("site_id").toString();
			
			if(moduleName.equals("news")) {
				for(int i=0;i<20;i++){
					Map saveData=new HashMap();
					saveData.put("name", menuName+"-测试资讯数据标题测试资讯数据标题测试资讯数据标题"+ (i+1));
					saveData.put("subname", menuName+"-测试资讯数据标题"+ (i+1)+"副标题");
					
					saveData.put("image", "default_img.jpg");
					saveData.put("author", "匿名");
					saveData.put("source", "互联网");
					saveData.put("hits", 0);
					saveData.put("digs", 0);
					saveData.put("menu_id",menuId);
					saveData.put("site_id",siteId);
					
					saveData.put("description", menuName+"-测试资讯数据简介测试资讯数据简介测试资讯数据简介测试资讯数据简介测试资讯数据简介测试资讯数据简介测试资讯数据简介测试资讯数据简介测试资讯数据简介");
					saveData.put("content", "<p>"+menuName+"-测试资讯数据正文<br/>测试资讯数据正文<br/>测试资讯数据正文<br/>测试资讯数据正文<br/>测试资讯数据正文<br/></p>");
					saveData.put("create_time",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) );
					saveData.put("position", new Date().getTime());
					this.getJdbcDao().addObj(DBTable.news, saveData);
				}
			}else if(moduleName.equals("links")) {
				for(int i=0;i<20;i++){
					Map saveData=new HashMap();
					saveData.put("name", menuName+"-标题"+ (i+1));
					saveData.put("image", "default_img.jpg");
					saveData.put("link_url", "http://www.baidu.com");
					saveData.put("menu_id",menuId);
					//saveData.put("site_id",siteId);
					saveData.put("create_time",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) );
					this.getJdbcDao().addObj(DBTable.links, saveData);
				}
			}else if(moduleName.equals("article")) {
				Map articleData=this.getJdbcDao().queryRow("select id from "+DBTable.article+" where menu_id=? and del_flag=0", new Object[] {menuId});
				if(articleData==null) {
					articleData=new HashMap();
					articleData.put("image", "default_img.jpg");
					articleData.put("menu_id",menuId);
					articleData.put("site_id",siteId);
					articleData.put("description", menuName+"-测试文章数据简介测试文章数据简介测试文章数据简介测试文章数据简介测试文章数据简介测试文章数据简介测试文章数据简介测试文章数据简介测试文章数据简介");
					articleData.put("content", "<p>"+menuName+"-测试文章数据正文</p>");
					articleData.put("create_time",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) );
					this.getJdbcDao().addObj(DBTable.article, articleData);
				}
				
				
			}
		}
		
		
		
	}

	

}
